﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmEducationInfo : Form
    {
        DialogResult dlgResult;
        BranchClient wcfBranch;
        DepartmentClient wcfDP;
        EmployeeClient wcfEmployee;
        int _EducationID;
        int _EmployeeID;
        bool isChange = false;
        EducationClient wcfEducation;
        EducationInfo _EducationInfo;
        EmployeeInfo _EmployeeInfo;
        RoleRightInfo listRole;
        public frmEducationInfo(int EducationID, int employeeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            dlgResult = DialogResult.Cancel;
            this.KeyPreview = true;
            Language();
            _EducationID = EducationID;
            _EmployeeID = employeeID;
            wcfEducation = Service.Education();
            _EducationInfo = new EducationInfo();
            wcfEmployee = Service.Employee();
            _EmployeeInfo = new EmployeeInfo();
            _EducationID = EducationID;
            if (_EducationID > 0)
            {
                _EducationInfo = wcfEducation.Education_GetInfo(_EducationID);
                Info2Form();
                GetEmployeeName(_EducationInfo.EmployeeID);
            }
            if (employeeID > 0)
            {
                GetEmployeeName(employeeID);
                btnSelectEmp.Visible = false;
            }
            else
                btnSelectEmp.Visible = true;

            listRole = _listRole;
        }

        private void Language()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }
        void GetEmployeeName(int employeeid)
        {
            _EmployeeInfo = wcfEmployee.Employee_GetInfo(employeeid);
            txtEmpName.Tag = _EmployeeInfo.ID;
            txtEmpName.Text = _EmployeeInfo.LastName + " " + _EmployeeInfo.FirstName;
        }
        void Info2Form()
        {
            txtName.Text = _EducationInfo.Name;
            txtEmpName.Tag = _EducationInfo.EmployeeID;
            cboKind.Text = _EducationInfo.Kind;
            txtMajor.Text = _EducationInfo.Major;
            chkForeign.Checked = _EducationInfo.Foreign ? true : false;
            txtPlace.Text = _EducationInfo.Place;
            txtFromDate.Text = _EducationInfo.FromDate == DateTime.MinValue ? "" : _EducationInfo.FromDate.ToString("dd/MM/yyyy");
            txtTime.Text = _EducationInfo.Time;
            txtDateIssue.Text = _EducationInfo.DateIssue == DateTime.MinValue ? "" : _EducationInfo.DateIssue.ToString("dd/MM/yyyy");
            txtAuthorIssue.Text = _EducationInfo.AuthorIssue;
            txtRank.Text = _EducationInfo.Rank;
            txtDescription.Text = _EducationInfo.Description;
            isChange = false;
        }

        void Form2Info()
        {
            _EducationInfo.BranchID = 0;
            _EducationInfo.DepartmentID = 0;
            _EducationInfo.EmployeeID = Convert.ToInt32(txtEmpName.Tag);
            _EducationInfo.Name = txtName.Text;
            _EducationInfo.Kind = cboKind.Text;
            _EducationInfo.Major = txtMajor.Text;
            _EducationInfo.Foreign = chkForeign.Checked ? true : false;
            _EducationInfo.Place = txtPlace.Text;
            _EducationInfo.FromDate = UISetting.GetDate(txtFromDate.Text);
            _EducationInfo.Time = txtTime.Text;
            _EducationInfo.DateIssue = UISetting.GetDate(txtDateIssue.Text);
            _EducationInfo.AuthorIssue = txtAuthorIssue.Text;
            _EducationInfo.Rank = txtRank.Text;
            _EducationInfo.Description = txtDescription.Text;
        }

        bool CheckValid()
        {
            if (txtEmpName.Text.Length == 0)
            {
                MessageBox.Show(UpdateUI.GetString("chuachonnhanvien"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtEmpName.Focus();
                return false;
            }

            //if (!CheckTextValid.IsDateTime(txtFromDate.Text, "dd/MM/yyyy"))
            //{
            //    txtFromDate.Focus();
            //    MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            if (!CheckTextValid.IsDateTime(txtDateIssue.Text, "dd/MM/yyyy"))
            {
                txtDateIssue.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaycapbangkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_EducationInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _EducationID = 0;
            _EducationInfo = new EducationInfo();
            txtFromDate.Text = "";
            txtDateIssue.Text = "";
            txtAuthorIssue.Text = "";
            txtDescription.Text = "";
            txtMajor.Text = "";
            txtName.Text = "";
            txtPlace.Text = "";
            txtTime.Text = "";
            txtRank.Text = "";
            chkForeign.Checked = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            Form2Info();
            if (_EducationID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfEducation.Education_Update(_EducationInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                    wcfEducation.Close();
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return; ;
                }
                _EducationInfo.ID = wcfEducation.Education_Insert(_EducationInfo);
                _EducationID = _EducationInfo.ID;
                if (_EducationID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                    wcfEducation.Close();
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmEducationInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            this.DialogResult = dlgResult;
        }

        private void frmEducationInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Close();
                    this.Dispose();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void frmEducationInfo_Activated(object sender, EventArgs e)
        {
            Language();
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void btnSelectEmp_Click(object sender, EventArgs e)
        {
            frmEmployeeList f = new frmEmployeeList(true);
            if (f.ShowDialog() == DialogResult.OK)
            {
                GetEmployeeName(f.EmpID);
            }
        }

    }
}
